<template>
  <div class="view-account">
    <div class="view-account-header"></div>
    <div class="view-account-container">
      <div class="view-account-top">
        <div class="view-account-top-logo">
          <!--todo 公司logo占位图-->
          <img src="" alt="公司logo占位图" />
        </div>
        <div class="view-account-top-desc">Naive-Admin CMS一站式运营管理平台</div>
      </div>
      <div class="main-box">
        <div :class="['container', 'container-register', { 'is-txl': isLogin }]">
          <div class="view-account-form">
            <n-form label-placement="left" size="large" :model="formInline">
              <n-form-item path="tenantCode">
                <n-input v-model:value="formInline.tenantCode" placeholder="请输入租户编码">
                  <template #prefix>
                    <n-icon size="18" color="#808695">
                      <ServerSharp />
                    </n-icon>
                  </template>
                </n-input>
              </n-form-item>
              <n-form-item path="username">
                <n-input v-model:value="formInline.username" placeholder="请输入用户名">
                  <template #prefix>
                    <n-icon size="18" color="#808695">
                      <PersonOutline />
                    </n-icon>
                  </template>
                </n-input>
              </n-form-item>
              <n-form-item path="password">
                <n-input
                  v-model:value="formInline.password"
                  type="password"
                  showPasswordOn="click"
                  placeholder="请输入密码"
                >
                  <template #prefix>
                    <n-icon size="18" color="#808695">
                      <LockClosedOutline />
                    </n-icon>
                  </template>
                </n-input>
              </n-form-item>
            </n-form>
          </div>
        </div>
        <div :class="['container', 'container-login', { 'is-txl is-z200': isLogin }]">
          <div class="view-account-form">
            <n-form
              ref="formRef"
              label-placement="left"
              size="large"
              :model="formInline"
              :rules="rules"
            >
              <n-form-item path="tenantCode">
                <n-input v-model:value="formInline.tenantCode" placeholder="请输入租户编码">
                  <template #prefix>
                    <n-icon size="18" color="#808695">
                      <ServerSharp />
                    </n-icon>
                  </template>
                </n-input>
              </n-form-item>
              <n-form-item path="username">
                <n-input v-model:value="formInline.username" placeholder="请输入用户名">
                  <template #prefix>
                    <n-icon size="18" color="#808695">
                      <PersonOutline />
                    </n-icon>
                  </template>
                </n-input>
              </n-form-item>
              <n-form-item path="password">
                <n-input
                  v-model:value="formInline.password"
                  type="password"
                  showPasswordOn="click"
                  placeholder="请输入密码"
                >
                  <template #prefix>
                    <n-icon size="18" color="#808695">
                      <LockClosedOutline />
                    </n-icon>
                  </template>
                </n-input>
              </n-form-item>
              <n-form-item path="code">
                <n-input v-model:value="formInline.code" placeholder="请输入验证码">
                  <template #prefix>
                    <n-icon size="18" color="#808695">
                      <QrCode />
                    </n-icon>
                  </template>
                  <template #suffix>
                    <img
                      v-show="true"
                      :src="formState.captchaSrc"
                      @click="loadCaptcha"
                      alt="captcha"
                      class="code-image"
                    />
                  </template>
                </n-input>
              </n-form-item>
              <n-form-item class="default-color">
                <div class="flex justify-between">
                  <div class="flex-initial">
                    <n-checkbox v-model:checked="autoLogin">自动登录</n-checkbox>
                  </div>
                  <div class="flex-initial order-last">
                    <a href="javascript:">忘记密码</a>
                  </div>
                </div>
              </n-form-item>
              <n-form-item>
                <n-button
                  type="primary"
                  @click="handleSubmit"
                  size="large"
                  :loading="loading"
                  block
                >
                  登录
                </n-button>
              </n-form-item>
              <!--              <n-form-item class="default-color">-->
              <!--                <div class="flex view-account-other">-->
              <!--                  <div class="flex-initial">-->
              <!--                    <span>其它登录方式</span>-->
              <!--                  </div>-->
              <!--                  <div class="flex-initial mx-2">-->
              <!--                    <a href="javascript:">-->
              <!--                      <n-icon size="24" color="#2d8cf0">-->
              <!--                        <LogoGithub />-->
              <!--                      </n-icon>-->
              <!--                    </a>-->
              <!--                  </div>-->
              <!--                  <div class="flex-initial mx-2">-->
              <!--                    <a href="javascript:">-->
              <!--                      <n-icon size="24" color="#2d8cf0">-->
              <!--                        <LogoFacebook />-->
              <!--                      </n-icon>-->
              <!--                    </a>-->
              <!--                  </div>-->
              <!--                </div>-->
              <!--              </n-form-item>-->
            </n-form>
          </div>
        </div>
        <div :class="['switch', { login: isLogin }]">
          <div class="switch__circle"></div>
          <div class="switch__circle switch__circle_top"></div>
          <div class="switch__container">
            <h2>{{ isLogin ? 'Hello Friend !' : 'Welcome Back !' }}</h2>
            <p>
              {{ isLogin ? '注册功能暂未开放 !' : '请使用账号密码登录 !' }}
            </p>
            <div class="primary-btn" @click="isLogin = !isLogin">
              {{ isLogin ? '立即注册' : '立即登录' }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { onMounted, reactive, ref } from 'vue';
  import { useRoute, useRouter } from 'vue-router';
  import { useUserStore } from '@/store/modules/user';
  import { useMessage } from 'naive-ui';
  import { ResultEnum } from '@/enums/httpEnum';
  import { ServerSharp, PersonOutline, LockClosedOutline, QrCode } from '@vicons/ionicons5';
  import { PageEnum } from '@/enums/pageEnum';
  import { buildUUID } from '@/utils/uuid';

  interface FormState {
    username: string;
    password: string;
    grant_type: string;
    client_id: string;
    client_secret: string;
    scope: string;
    tenant_code: string;
    auth_type: string;
    vc_code: string;
    vc_token: string;
  }

  const isLogin = ref<Boolean>(true);

  const formRef = ref();
  const message = useMessage();
  const loading = ref(false);
  const autoLogin = ref(true);
  const LOGIN_NAME = PageEnum.BASE_LOGIN_NAME;

  const formInline = reactive({
    username: 'admin',
    password: '123456',
    tenantCode: '0000',
    key: buildUUID(),
    code: '',
  });

  const formState = reactive({
    captchaSrc: '',
  });

  const rules = {
    tenantCode: { required: true, message: '请输入租户编码', trigger: 'blur' },
    username: { required: true, message: '请输入用户名', trigger: 'blur' },
    password: { required: true, message: '请输入密码', trigger: 'blur' },
    code: { required: true, message: '请输入验证码', trigger: 'blur' },
  };

  onMounted(() => {
    loadCaptcha();
  });

  const userStore = useUserStore();

  const router = useRouter();
  const route = useRoute();

  // 加载验证码
  async function loadCaptcha() {
    formInline.key = buildUUID();
    formState.captchaSrc = '/api/authority/captcha?key=' + formInline.key;
  }

  const handleSubmit = (e) => {
    e.preventDefault();
    formRef.value.validate(async (errors) => {
      if (!errors) {
        const { username, password, tenantCode, code, key } = formInline;
        message.loading('登录中...');
        loading.value = true;

        const params: FormState = {
          username,
          password,
          grant_type: 'password',
          client_id: 'client',
          client_secret: 'client',
          scope: 'server',
          tenant_code: tenantCode,
          auth_type: 'vc',
          vc_code: code,
          vc_token: key,
        };

        try {
          const { code, message: msg } = await userStore.login(params);
          message.destroyAll();
          if (code == ResultEnum.SUCCESS) {
            const toPath = decodeURIComponent((route.query?.redirect || '/') as string);
            message.success('登录成功，即将进入系统');
            if (route.name === LOGIN_NAME) {
              await router.replace('/');
            } else await router.replace(toPath);
          } else {
            message.info(msg || '登录失败');
          }
        } finally {
          loading.value = false;
        }
      } else {
        message.error('请填写完整信息，并且进行验证码校验');
      }
    });
  };
</script>

<style lang="scss" scoped>
  .view-account {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: auto;

    &-container {
      flex: 1;
      padding: 32px 12px;
      max-width: 1000px;
      min-width: 320px;
      margin: 0 auto;
    }

    &-top {
      padding: 32px 0;
      text-align: center;

      &-desc {
        font-size: 14px;
        color: #808695;
      }
    }

    &-other {
      width: 100%;
    }

    .default-color {
      color: #515a6e;

      .ant-checkbox-wrapper {
        color: #515a6e;
      }
    }
  }

  .main-box {
    display: flex;
    position: relative;
    width: 1000px;
    min-width: 1000px;
    min-height: 600px;
    height: 600px;
    padding: 25px;
    background-color: #ecf0f3;
    box-shadow: 10px 10px 10px #d1d9e6, -10px -10px 10px #f9f9f9;
    border-radius: 12px;
    overflow: hidden;
    .container {
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      top: 0;
      width: 600px;
      height: 100%;
      padding: 25px;
      background-color: #ecf0f3;
      transition: all 1.25s;
    }
    .container-register {
      z-index: 100;
      left: calc(100% - 600px);
    }
    .container-login {
      left: calc(100% - 600px);
      z-index: 0;
    }
    .is-txl {
      left: 0;
      transition: 1.25s;
      transform-origin: right;
    }
    .is-z200 {
      z-index: 200;
      transition: 1.25s;
    }
    .switch {
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      top: 0;
      left: 0;
      height: 100%;
      width: 400px;
      padding: 50px;
      z-index: 200;
      transition: 1.25s;
      background-color: #ecf0f3;
      overflow: hidden;
      box-shadow: 4px 4px 10px #d1d9e6, -4px -4px 10px #f9f9f9;
      color: #a0a5a8;
      .switch__circle {
        position: absolute;
        width: 500px;
        height: 500px;
        border-radius: 50%;
        background-color: #ecf0f3;
        box-shadow: inset 8px 8px 12px #d1d9e6, inset -8px -8px 12px #f9f9f9;
        bottom: -60%;
        left: -60%;
        transition: 1.25s;
      }
      .switch__circle_top {
        top: -30%;
        left: 60%;
        width: 300px;
        height: 300px;
      }
      .switch__container {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        position: absolute;
        width: 400px;
        padding: 50px 55px;
        transition: 1.25s;
        h2 {
          font-size: 34px;
          font-weight: 700;
          line-height: 3;
          color: #181818;
        }
        p {
          font-size: 14px;
          letter-spacing: 0.25px;
          text-align: center;
          line-height: 1.6;
        }
      }
    }
    .login {
      left: calc(100% - 400px);
      .switch__circle {
        left: 0;
      }
    }
    .primary-btn {
      width: 180px;
      height: 50px;
      border-radius: 25px;
      margin-top: 50px;
      text-align: center;
      line-height: 50px;
      font-size: 14px;
      letter-spacing: 2px;
      background-color: #4b70e2;
      color: #f9f9f9;
      cursor: pointer;
      box-shadow: 8px 8px 16px #d1d9e6, -8px -8px 16px #f9f9f9;
      &:hover {
        box-shadow: 4px 4px 6px 0 rgb(255 255 255 / 50%), -4px -4px 6px 0 rgb(116 125 136 / 50%),
          inset -4px -4px 6px 0 rgb(255 255 255 / 20%), inset 4px 4px 6px 0 rgb(0 0 0 / 40%);
      }
    }
  }

  @media (min-width: 768px) {
    .view-account {
      background-image: url('../../../assets/images/login.svg');
      background-repeat: no-repeat;
      background-position: 50%;
      background-size: 100%;
    }

    .page-account-container {
      padding: 32px 0 24px 0;
    }
  }
</style>
